<?
include_once ("../config.php");
include_once ("../class/class_database.php");
include_once ("../class/tbs_class.php");
include_once ("../class/class_common.php");
include_once ("../class/class_login.php");


$script  = "\nvar menu = new TMainMenu('menu','horizontal');"."\n";
$script .= GenMenuVar ($user_id);
$script .= GenMenuEngine ($user_id,$parent_id,$parent_name);
print $script;

function GenMenuVar ($user_id) {
	global $DBConnection, $MOD_DIR;
	$SQL = "SELECT module_name, module_order, module_script, module_path, module_id, module_desc, parent_module_id FROM tblmodules ";
	if ($parent_id) $SQL .= "WHERE parent_module_id=".$parent_id;
	$SQL .= " ORDER BY module_order";
	$PullDownMenu = $DBConnection->dbc->get_results($SQL);
	$script = "";
	if ($PullDownMenu) {
		foreach ( $PullDownMenu as $PDM ) {
		 // Access data using object syntax
			$PulldownName = str_replace(array(" ",".","/"),"",$PDM->module_name.$PDM->module_order);
			if ($PDM->module_script) {
				$LinkParam="a";
				if ($PDM->module_path) $ModuleScript = $MOD_DIR."/".$PDM->module_path."/".$PDM->module_script;
				else $ModuleScript = $MOD_DIR."/".$PDM->module_script;
		    }
			else $LinkParam="";
			if (!$PDM->parent_module_id) $ParentMode = 0;
	 	    $script .= "var pm".$PulldownName." = "."new TPopMenu('".$PDM->module_name."','$ParentMode','$LinkParam','".$ModuleScript."','".$PDM->module_desc."');"."\n";
		}
	}
	return $script;
}

function GenMenuEngine ($user_id,$parent_id,$parent_name) {
    $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    SelectModuleByGroup(GetGroupID());
	$SQL = "SELECT tblmodules.module_name, tblmodules.module_order, tblmodules.module_script, tblmodules.module_path, tblmodules.module_id, tblmodules.parent_module_id, tblaccess_bygroup.noaccess ".
		   "FROM tblmodules LEFT JOIN tblaccess_bygroup ON tblmodules.module_id=tblaccess_bygroup.module_id ";
	if ($parent_id) $SQL .= "WHERE parent_module_id=".$parent_id;
	else $SQL .= "WHERE ISNULL(parent_module_id)";
	$SQL .= " ORDER BY module_order";
	//print $SQL."<br>\n";
	$PullDownMenu = $DBConnection->dbc->get_results($SQL);
	$script = "";
	if ($PullDownMenu) {
		foreach ($PullDownMenu as $PDM ) {
			if ($PDM->noaccess==0) {
				//print ChildCount($PDM->module_id)."=".IsAccessedChild($PDM->module_id)."<br>";
				$ModulesCount = $ModulesCount + IsAccessedChild($PDM->module_id);
				if (((ChildCount($PDM->module_id)>0) && ($ModulesCount<>0)) || (strlen($PDM->module_script))) {
					$PulldownName = str_replace(array(" ",".","/"),"",$PDM->module_name.$PDM->module_order);
				 	if (!$PDM->parent_module_id) {
						$script .= "menu.Add(pm".$PulldownName.");"."\n";
				 	} else {
				 		$script .= "pm".$parent_name.".Add(pm".$PulldownName.");\n";
				 	}
					$script .= GenMenuEngine ($user_id,$PDM->module_id,$PulldownName);
				}
			}
		}
	}
	return $script;
}

function ChildCount($parent_module_id) {
	$DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
	return $DBConnection->dbc->get_var("SELECT COUNT(*) FROM tblmodules WHERE parent_module_id=$parent_module_id");
}

function IsAccessedChild($parent_module_id) {
	$DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
	$SQL = "SELECT COUNT(*) ".
		   "FROM tblmodules ".
		   "INNER JOIN tblaccess_bygroup ON tblmodules.module_id=tblaccess_bygroup.module_id ".
		   "WHERE parent_module_id=$parent_module_id AND noaccess=0";
	//print $SQL."<br><br>\n\n";
	return $DBConnection->dbc->get_var($SQL);
}

function SelectModuleByGroup($GroupID) {
	global $DBConnection;
	$SQL = "DROP TABLE IF EXISTS `tblaccess_bygroup`;";
	$DBConnection->dbc->query($SQL);
	$SQL = "
	CREATE TEMPORARY TABLE `tblaccess_bygroup` (
	  `group_id` int(11) default NULL,
	  `module_id` int(11) default NULL,
	  `insert` tinyint(3) unsigned NOT NULL default '0',
	  `update` tinyint(3) unsigned NOT NULL default '0',
	  `delete` tinyint(3) unsigned NOT NULL default '0',
	  `noaccess` tinyint(3) unsigned NOT NULL default '0',
	  `approval1` tinyint(3) unsigned NOT NULL default '0',
          `approval2` tinyint(3) unsigned NOT NULL default '0',
          `approval3` tinyint(3) unsigned NOT NULL default '0'
	) TYPE=MyISAM;
	 ";
	$DBConnection->dbc->query($SQL);
	$SQL = "INSERT INTO tblaccess_bygroup SELECT * FROM tblaccess WHERE group_id=".$GroupID;
	$DBConnection->dbc->query($SQL);
	
}
?>